<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Dcat\Admin\Layout\Content;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Models\Cabinet;
use App\Models\CabinetAlarmLog;
use App\Handlers\WeconHandler;
use Dcat\Admin\Admin;


class NewIndexController extends Controller
{
    public function index(Content $content){
        Admin::style(
        <<<CSS
            .amap-marker-label {
                border-style: none;
                background-color: rgba(0,0,0,1);
            }
        CSS);

        Admin::css([
            '/new-index/css/index.css',
            '/new-index/fonts/icomoon.css',
        ]);

        $projectIds = ProjectUser::where('user_id', Admin::user()->id)->pluck('projectId')->toArray();
        //地图数据相关
        $projects = Project::select(['projectId','projectname', 'lat', 'lng'])->whereIn('projectId', $projectIds)->get()->toArray();
        // print_r($projects);die;
        $mapList = [];
        $provinces = [];
        $urlPrefix = 'https://restapi.amap.com/v3/geocode/regeo?output=json&location=';
        $urlSuffix = '&key=efc9084469fb60d4aaef0dfe90c7bdf5&extensions=base';
        $AlarmProject = []; //报警项目
        $Project = []; //在线项目
        $address = [];
        foreach ($projects as $one) {
            if (!empty($one['lat'])  && !empty($one['lng'])) {
                $address[$one['projectname']] = array($one['lng'],$one['lat']);
                $t['name'] = $one['projectname'];
                $t['lat'] = $one['lat'];
                $t['lng'] = $one['lng'];
                $t['id'] = $one['projectId'];
                $a['name'] = $one['projectname'];
                $undealLogNum = CabinetAlarmLog::where('project_id', $one['projectId'])->where('status', CabinetAlarmLog::STATUS_NO)->count();
                $boxId = Cabinet::where('project_id', $one['projectId'])->where('pid', 0)->value('id');
                $t['url'] = $boxId ? admin_url('cabinet-children').'/'.$boxId : '';
                $a['content'] = $boxId ? 'cabinet-children/'.$boxId : 'project/'.$one['projectId'];
                if ($undealLogNum > 0) {
                    $t['icon'] = url('icon/warning.png');
                    $a['value'] = 133;
                    array_push($AlarmProject, $a);
                } else {
                    $t['icon'] = '';
                    $a['value'] = 133;
                    array_push($Project, $a);
                }
                array_push($mapList, $t);
                if (intval($one['lat']) && intval($one['lng'])) {
                    $url = $urlPrefix.$one['lng'].','.$one['lat'].$urlSuffix;
                    $rr = Request_curl($url, false, 0, 0);
                    $r = json_decode($rr, true);
                    if ($r['status'] && $r['regeocode']['addressComponent']['province']) {
                        $oneP = $r['regeocode']['addressComponent']['province'];
                        array_push($provinces, $oneP);
                    }
                }
            }
        }
        $data['mapList'] = $mapList;
        $data['address'] = $address;
        $data['alarmproject'] = $AlarmProject;
        $data['inlineproject'] = $Project;
        //省份分布
        $tmpProvince = array_count_values($provinces);
        $provinceData = [];
        foreach ($tmpProvince as $pname => $pnum) {
            $tmpOne['value'] = $pnum;
            $tmpOne['name'] = $pname;
            array_push($provinceData, $tmpOne);
        }
        $data['provinceList']['num'] = count($tmpProvince);
        $data['provinceList']['data'] = json_encode($provinceData);

        //在线设备数据
        $boxes = Cabinet::whereIn('project_id',$projectIds)->where('pid', 0)->pluck('machine_code', 'id')->toArray();
        $total = count($boxes);
        $online = 0;
        $offline = 0;
        if ($boxes) {
            foreach ($boxes as $id => $machineCode) {
                $check = WeconHandler::getBoxStatus($machineCode);
                if ($check) {
                    $online++;
                } else {
                    $offline++;
                }
            }
        }
        $boxStatus['total'] = $total;
        $boxStatus['online'] = $online;
        $boxStatus['offline'] = $offline;
        $data['boxStatus'] = $boxStatus;
        //报警记录
        $alertLogList = CabinetAlarmLog::select('alarm_name', 'created_at')->whereIn('project_id',$projectIds)->orderBy('created_at', 'desc')->take(10)->get()->toArray();
        foreach ($alertLogList as &$one) {
            $one['created_at'] = $one['created_at'];
            $one['msg'] = $one['alarm_name'];
        }
        $data['alertLogList'] = $alertLogList;
        // print_r($data);die;
        return $content
            ->header('')
            ->description('')
            ->body(view('admin.newIndex.indexs', $data));
    }
}
